package com.imoresoft.magic.action;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.Map;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.sql.dao.impl.SuperDaoImpl;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.imoresoft.magic.entity.Result;
import com.imoresoft.magic.property.AppConstants;

public class SettingPersistAction extends BaseAction {

	private static final long serialVersionUID = -2528418471608896211L;

	private Map<String, String> settings;

	private String itemJsonStr;

	private Result result = new Result();

	private SuperDao superDao = null;

	public SettingPersistAction() {
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	public Map<String, String> getSettings() {
		return settings;
	}

	public void setSettings(Map<String, String> settings) {
		this.settings = settings;
	}

	public String getItemJsonStr() {
		return itemJsonStr;
	}

	public void setItemJsonStr(String itemJsonStr) {
		this.itemJsonStr = itemJsonStr;
	}

	@SuppressWarnings("unchecked")
	public String persist() throws UnsupportedEncodingException, SQLException {
		superDao = new SuperDaoImpl(pd.getConn());

		try {
			IDataset itemList = new DatasetList(this.getItemJsonStr());
			IData data = new DataMap(settings);
			data.put("NICK", pd.getTopNick());
			data.put("USER_ID", pd.getTopUserId());
			//data.put("BUILD_HTML", buildDisplayHtml(itemList, data));
			String ruleType = settings.get("RULE_TYPE");
			if ("TF_M_USER_RULE_RANDOM".equals(ruleType)) {
				IData dp = new DataMap();
				dp.put("USER_ID", pd.getTopUserId());
				superDao.delete("TF_M_USER_RULE_RANDOM", dp,
						new String[] { "USER_ID" });
			}
			String ruleId = superDao
					.insertAndReturnLastInsertId(ruleType, data);

			Iterator ite = itemList.iterator();
			while (ite.hasNext()) {
				IData d = (IData) ite.next();
				d.put("NICK", pd.getTopNick());
				d.put("USER_ID", pd.getTopUserId());
				d.put("RULE_ID", ruleId);
				d.put("RULE_TYPE", ruleType);
			}
			superDao.insert("TF_M_USER_RULE_ITEM", itemList);
			superDao.delete("TF_M_USER_ITEM", itemList);
			superDao.insert("TF_M_USER_ITEM", itemList);
			int ef = pd.getRequestParameter("ef") == null ? 0 : Integer
					.parseInt(pd.getRequestParameter("ef"));
			if (AppConstants.RULE_TAKE_EFFECT == ef) {
				pd.setUserRuleType(settings.get("RULE_TYPE"));
				data.clear();
				data.put("RULE_TYPE", settings.get("RULE_TYPE"));
				data.put("USER_ID", pd.getTopUserId());
				superDao.update("TF_M_USER", data);
			}
		} catch (SQLException e) {
			logger.warn("数据库操作出错：", e);
			result.setCode(-1);
			result.setMsg("保存规则-插入数据库时出错");
		} catch (Exception e) {
			logger.error("", e);
			result.setCode(-1);
			result.setMsg("保存规则时出错，请刷新页面重新操作");
		}

		return SUCCESS;
	}
}
